<template>
  <div>
    <FormPanel ref="topFilter" :list="topFilterList" :isSubmitBtn="false"></FormPanel>
    <FilterTable
      ref="table"
      columnsRef="allocationInShowItemsRef"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onCellChange="changeAmount"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <div class="btnWrap">
      <el-button @click="closeDrawer(1)">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction } from '@/utils';
import { getUser, getUserId } from '@/assets/js/auth';
import { getCangKuSelect } from '@/api/part/partOutbound/repairMaterials/dict.js';
import { saveAllocationIn, queryAllocationIn, queryAllocationInItems } from '@/api/part/allocation';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import moment from 'moment';
import { jsonToHump } from '@/utils/index';

export default {
  mixins: [dictionary],
  props: {
    data: Object
  },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      isShowAddPart: false,
      fetchapi: queryAllocationInItems,
      kehuxuanze: '',
      rowData: {},
      isShowSearchCustomer: false,
      fetchParams: { xhrAbort: true },
      isShowSearchOldAllcation: false,
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    console.log('data', this.data);
    this.getAllUser();
    this.getAllStorage();
    this.init();
  },
  methods: {
    addPart() {},
    delete(val) {
      this.$refs.table.EXECUTE_DELETE(val);
    },
    confirmSearchCustomer() {},
    async closeDrawer(val) {
      this.$emit('close', false);
    },
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '入库单号',
          disabled: true,
          initialValue: '',
          fieldName: 'allocateInNo'
        },
        {
          type: 'INPUT',
          label: '对方经销商',
          initialValue: '',
          disabled: true,
          fieldName: 'customerCode'
        },
        {
          type: 'INPUT',
          label: '原单据号',
          initialValue: '',
          disabled: true,
          fieldName: 'oldAllocateOutNo'
        },
        {
          type: 'SELECT',
          label: '经办人',
          disabled: true,
          initialValue: '',
          fieldName: 'handler',
          disabled: true,
          itemList: []
        },
        {
          type: 'INPUT',
          label: '经办人电话',
          initialValue: '',
          fieldName: 'handlerPhone',
          disabled: true
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'orderDate',
          initialValue: '',
          disabled: true,
          valueFormat: 'yyyy-MM-dd',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '申请备注',
          initialValue: '',
          fieldName: 'remark',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          initialValue: '',
          fieldName: 'examinedRemark',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '仓库名称',
          dataIndex: 'storageCode',
          initialValue: '',
          dictItems: [],
          sorter: true
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          initialValue: '',
          sorter: true
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          initialValue: '',
          sorter: true
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          initialValue: '',
          sorter: true
        },
        {
          title: '调拨数量',
          dataIndex: 'inQuantity',
          initialValue: 0,
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number'
        },
        {
          title: '调拨单价',
          filterType: 'number',
          initialValue: 0,
          dataIndex: 'inPrice',
          precision: 2,
          sorter: true
        },
        {
          title: '调拨金额',
          initialValue: '',
          filterType: 'number',
          initialValue: 0,
          precision: 2,
          dataIndex: 'inAmount',
          initialValue: 0,
          sorter: true
        }
      ];
    },
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.topFilterList.find(item => item.fieldName === 'handler').itemList = data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    changeAmount(val, row) {
      console.log('val::', val, 'row::', row);
      row.inAmount = val[Object.keys(val)[0]] * row.inPrice;
      console.log('val1::', val, 'row::1', row);
    },
    collapseHandle() {
      this.refs.table.EXECUTE_RESET_HEIGHT();
    },
    async getAllStorage() {
      let res = await getCangKuSelect({
        // type: this.type == 'add' ? 0 : '', // 0是非oem 0是oem  EP-23605 去除入库oem类型限制
        // ptype: this.ptype ? this.ptype : null // ,模块区分
      });
      let storageData = res.data.map(item => ({ text: item.STORAGE_NAME, value: item.STORAGE_CODE }));
      this.columns.find(item => item.dataIndex == 'storageCode').dictItems = storageData;
    },
    async init() {
      await this.topFilterList.find(x => {
        if (x.fieldName == 'customerCode') {
          this.rowData = { dealerCode: this.data['customerCode'], dealerName: this.data['customerName'] };
          x.initialValue = this.data['customerName'];
        } else {
          x.initialValue = this.data[x.fieldName];
        }
      });
      this.fetchParams = this.fetchParams = Object.assign({ allocateInNo: this.data.allocateInNo });
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
